

#include <stdio.h>
#include <stdlib.h>

struct ListNode{
    int val;
    struct ListNode *next;
};

typedef struct ListNode Node, *pNode;

void print(struct ListNode* p)
{
	struct ListNode* q;
	for(q = p; q != NULL; q = q->next)
		printf("[%d]", q->val);
	printf("\n");
}

void setList(struct ListNode * p, int val)
{
	struct ListNode *q;
	q = p;
	while(q->next != NULL)
		q = q->next;
	
	struct ListNode *tmp = (struct ListNode *)malloc(sizeof(struct ListNode));
	if(tmp == NULL)
		return;
	tmp->val = val;
	tmp->next = q->next;
	q->next = tmp;
}

struct ListNode* swapPairs(struct ListNode* head) {
	struct ListNode* p;
	if(head == NULL || head->next == NULL)
		return head;
	p = head->next;
	head->next = swapPairs(p->next);
	p->next = head;
	return p;
}

int main(void)
{
	struct ListNode* head;
	int num[] = {1,2,3,4,5,6};
	int val = 1;
	int i;
	head = (struct ListNode *)malloc(sizeof(struct ListNode));
	head->val = num[0];
	head->next = NULL;

	for(i = 1; i < sizeof(num)/sizeof(int); i++)
		setList(head, num[i]);
	print(head);
	print(swapPairs(head));
	return 0;
}